package cn.woblog.android.common.db;

import android.content.Context;

import com.j256.ormlite.dao.Dao;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.woblog.android.downloader.bean.DownloadInfo;
import cn.woblog.android.downloader.bean.DownloadStatus;

/**
 * Created by ren on 2015/5/5 0005.
 */
public class DBController {
    private static final String SQL_INIT_DOWNLOAD_STATUS = "update download_info set status='PAUSE' where status != 'DONE'";
    private static DBHelper helper;
    public static DBController instance;

    public DBController(Context context) {
        helper = new DBHelper(context);
    }

    public static DBController getInstance(Context context) {
        if (instance == null){
            instance = new DBController(context);
        }
        return instance;
    }
    public void initAllDownloadInfoStatus() {
        try {
            Dao<DownloadInfo, Long> examDao = helper
                    .getDao(DownloadInfo.class);
            examDao.executeRaw(SQL_INIT_DOWNLOAD_STATUS);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<DownloadInfo> queryAllNotDownloadComplete() {
        return queryDownloadInfos(DownloadStatus.DONE);
    }

    public List<DownloadInfo> queryAllDownloadComplete() {
        try {
            Dao<DownloadInfo, Long> examDao = helper
                    .getDao(DownloadInfo.class);
            ArrayList<Long> arrayList = null;
            List<DownloadInfo> query = examDao
                    .queryBuilder()
                    .orderBy("cTime", true)
                    .where()
                    .eq("status",
                            DownloadStatus.DONE)
                    .query();
            if (query != null && query.size() > 0) {
                int size = query.size();
                arrayList = new ArrayList<Long>();
                for (int i = 0; i < size; i++) {
                    DownloadInfo downloadInfo = query.get(i);
                    if (!new File(downloadInfo.getPath()).exists()) {
                        arrayList.add(downloadInfo.getId());
                    }
                }
            }
            if (arrayList != null && arrayList.size() > 0) {
                int size = arrayList.size();
                for (int i = 0; i < size; i++) {
                    examDao.deleteById(arrayList.get(i));
                }
                return examDao
                        .queryBuilder()
                        .orderBy("cTime", true)
                        .where()
                        .eq("status",
                               DownloadStatus.DONE)
                        .query();
            }
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<DownloadInfo>();
    }

    //com.haixue.app.Data.download.upgrade.DownloadStatus.DONE
    private List<DownloadInfo> queryDownloadInfos(DownloadStatus status) {
        try {
            Dao<DownloadInfo, Long> examDao = helper
                    .getDao(DownloadInfo.class);
            ArrayList<Long> arrayList = null;
            List<DownloadInfo> query = examDao.queryBuilder()
                    .orderBy("cTime", true).where().ne("status", status)
                    .query();
            if (query != null && query.size() > 0) {
                int size = query.size();
                arrayList = new ArrayList<Long>();
                for (int i = 0; i < size; i++) {
                    DownloadInfo downloadInfo = query.get(i);
                    if (!new File(downloadInfo.getPath()).exists()) {
                        arrayList.add(downloadInfo.getId());
                    }
                }
            }
            if (arrayList != null && arrayList.size() > 0) {
                int size = arrayList.size();
                for (int i = 0; i < size; i++) {
                    examDao.deleteById(arrayList.get(i));
                }
                return examDao.queryBuilder().orderBy("cTime", true).where()
                        .ne("status", status).query();
            }
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<DownloadInfo>();
    }

    public void newOrUpdate(DownloadInfo entry) {
        try {
            Dao<DownloadInfo, String> dao = helper
                    .getDao(DownloadInfo.class);
            if (entry.getStatus() != DownloadStatus.DELETE) {
                dao.createOrUpdate(entry);
            } else {
                dao.deleteById(entry.getId() + "");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public DownloadInfo queryDownloadInfo(long id) {
        try {
            Dao<DownloadInfo, Long> examDao = helper
                    .getDao(DownloadInfo.class);
            return examDao
                    .queryBuilder()
                    .where()
                    .eq("id", id)
                    .and()
                    .eq("status",
                            DownloadStatus.DONE)
                    .queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public int queryDownloadedSize() {
        try {
            Dao<DownloadInfo, Long> examDao = helper
                    .getDao(DownloadInfo.class);
            return (int) examDao
                    .queryBuilder()
                    .where()
                    .eq("status",
                            DownloadStatus.DONE)
                    .countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int deleteDownloadInfo(long id) {
        try {
            Dao<DownloadInfo, Long> examDao = helper
                    .getDao(DownloadInfo.class);
            return examDao.deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

//    public <T> List<T> queryDatas(Class<T> mClass){
//        try {
//            Dao<T, ?> dao = helper.getDao(mClass);
//            return   dao.queryForAll();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return  null;
//    }
//
//    public <T> void createOrUpdate(List<T> datas,Class<T> mClass){
//        try {
//            Dao<T, ?> dao = helper.getDao(mClass);
//            for (int i = 0; i <datas.size() ; i++) {
//                dao.createOrUpdate(datas.get(i));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }
}
